// ==UserScript==
// @name           Grantsfire Related Grants
// @namespace      http://grantsfire.com/userscripts/related
// @description    Show if there are any Grantsfire results for current URL
// @include        *
// ==/UserScript==

var gf_host = 'http://dev.solpath.org:3000'
var gf_url = gf_host + '/grant/search?q=' + escape(document.location.hostname);

if (!document.defaultView.frameElement) {

GM_xmlhttpRequest({
    method: 'GET',
    url: gf_url,
    headers: {
        'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
        'Accept': 'application/xhtml+xml',
    },
    onload: function(res) {
        var parser = new DOMParser();
        var doc = parser.parseFromString(res.responseText, "application/xhtml+xml");
	if (doc) {
            osE = doc.getElementsByTagNameNS('http://a9.com/-/spec/opensearch/1.1/', 'totalResults');		
            if (osE && osE[0]) {
                var d = document.createElement("div");
                d.innerHTML = '<a style="color: blue; font-size: 12px" href="'+  gf_url +'">' + osE[0].textContent + ' related grants</a> on Grantsfire';
                d.style.backgroundColor = '#EEEBAC';
                d.style.textAlign = 'center';
                d.style.paddingTop = '10px';
                d.style.paddingBottom = '10px';
                d.style.color = 'black';
                d.style.fontSize = '12px';
		d.style.fontFamily = 'Helvetica, Arial, sans-serif';
                var body = document.getElementsByTagName('body')[0];
                body.insertBefore(d, body.firstChild);
            }
	}
    }
});

}
